<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests;

use Illuminate\Http\Request;
use App\Model\AppUpgrade;

class AppUpgradeController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_xSystem' => 'am-in',
            '_xApp' => 'am-active',
        ]);
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {

        $apps = AppUpgrade::get();

        return view('Admin.xHotel.app.index')->with('apps', $apps);
    }


    /**
     * 修改显示页
     * @param $id
     */
    public function edit($id)
    {

        $app = AppUpgrade::find($id);

        return view('Admin.xHotel.app.edit')->with('app', $app);
    }

    /**
     *
     * @param $str
     * @return array|string
     */
    private function str_re($str)
    {

        $result = array();
        if ($str == "") {
            return "";
        }
        $result[] = explode('_', explode('/', $str)[4])[0];
        $result[] = explode('_', explode('.', explode('/', $str)[4])[0])[1];
        return $result;

    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update(Request $request, $id)
    {
        $app = AppUpgrade::find($id);
        //echo $app->url_athree;
        //return $request->url_athree;

        /*if (!$request->url_aone == "" && !$request->url_atwo == "" && !$request->url_athree == "")
        {
            if ($request->url_aone != $app->url_aone) {
                unlink(getcwd() . $app->url_aone);
            }

            if($request->url_atwo != $app->url_atwo){
                unlink(getcwd() . $app->url_atwo);
            }

            if($request->url_athree != $app->url_athree){
                unlink(getcwd() . $app->url_athree);
            }
        }*/
        //$data = $request->except(['url_aone','url_atwo','url_athree']);
        //
        $data['url_aone'] = $request->url_aone;
        $data['url_atwo'] = $request->url_atwo;
        $data['url_athree'] = $request->url_athree;

        $data['name_aone'] = $this->str_re($request->url_aone)[0];
        $data['name_atwo'] = $this->str_re($request->url_atwo)[0];
        $data['name_athree'] = $this->str_re($request->url_athree)[0];

        $data['number_aone'] = $this->str_re($request->url_aone)[1];
        $data['number_atwo'] = $this->str_re($request->url_atwo)[1];
        $data['number_athree'] = $this->str_re($request->url_athree)[1];

        $data['is_update_one'] = $request->is_update_one;
        $data['is_update_two'] = $request->is_update_two;
        $data['is_update_three'] = $request->is_update_three;
        
        $app->update($data);
        return redirect('/app/1/edit')->with('success', '编辑成功');
    }

    /**
     * 是否显示
     * @param Request $request
     */
    public function is_something(Request $request)
    {
        $attr = $request->attr;
        $hotelreferral = AppUpgrade::find($request->id);
        $value = $hotelreferral->$attr ? false : true;
        $hotelreferral->$attr = $value;
        $hotelreferral->save();
    }


}
